<template>
	<view class="my-v" v-if="loading">
		<view class="u-flex user-box u-p-l-32 u-p-r-22 u-p-b-30">
			<view class="u-m-r-20">
				<u-avatar size="140" @click='chooseAvatar' :src='avatarSrc'></u-avatar>
			</view>
			<view class="u-flex-1 f-right" @click="personalPage('/pages/my/personalData/index')">
				<view class="u-font-18">{{baseInfo.realName}}</view>
				<view class="u-m-l-10 u-p-10">
					<u-icon name="arrow-right" color="#fff" size="28" ></u-icon>
				</view>
			</view>
		</view>
		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item title="我的档案" @click="openPage('/pages/apply/dynamicModel/index')">
					<text class="icon-ym iconfont icon-record-full" slot="icon" />
				</u-cell-item>
				<u-cell-item title="我的证书" @click="openPage('/pages/apply/tezhongzuoye/index')">
					<text class="icon-ym iconfont icon-record-full" slot="icon" />
				</u-cell-item>
				<u-cell-item title="我的消息" @click="openPage('/pages/my/systemnews/index')">
					<text class="icon-ym iconfont icon-xiaoxi1" slot="icon" />
				</u-cell-item>
				<u-cell-item title="修改密码" @click="openPage('/pages/my/modifyPsd/index')">
					<text class="icon-ym iconfont icon-xiugaimima" slot="icon" />
				</u-cell-item>
				<u-cell-item title="技术支持" @click="openPage('/pages/my/abouts/index')">
					<text class="icon-ym iconfont icon-jishufuwu" slot="icon" />
				</u-cell-item>
			</u-cell-group>
		</view>
	<view class="out" @click="logout()">
		<span>退出登陆</span>
	</view>
	</view>
</template>

<script>
	import IndexMixin from './mixin.js'
	import {
		UpdateAvatar
	} from '@/api/common'
	import {
		UserSettingInfo
	} from '@/api/common'
	export default {
		mixins: [IndexMixin],
		data() {
			return {
				avatarSrc: '',
				baseInfo: {},
				loading: false
			}
		},
		computed: {
			baseURL() {
				return this.define.comUploadUrl
			},
			baseURL2() {
				return this.define.baseURL
			},
			token() {
				return this.$store.getters.token
			},
			report() {
				return this.define.report
			}
		},
		onShow() {
			UserSettingInfo().then(res => {
				this.baseInfo = res.data || {}
				this.avatarSrc = this.baseURL2 + this.baseInfo.avatar
				this.loading = true
			})
		},
		methods: {
			logout() {
				uni.showModal({
					title: '提示',
					content: '确定退出当前账号吗？',
					success: res => {
						if (res.confirm) {
							this.$store.dispatch('user/logout').then(() => {
								uni.closeSocket()
								uni.reLaunch({
									url: '/pages/login/index'
								})
							})
						}
					}
				})
			},
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					success: (res) => {
						let tempFilePaths = res.tempFilePaths[0]
						uni.uploadFile({
							url: this.baseURL + 'userAvatar',
							filePath: tempFilePaths,
							name: 'file',
							header: {
								'Authorization': this.token
							},
							success: (uploadFileRes) => {
								let data = JSON.parse(uploadFileRes.data)
								UpdateAvatar(data.data.name).then(res => {
									this.$u.toast('头像更换成功')
									this.avatarSrc = this.baseURL2 + data.data.url
								})
							},
							fail: (err) => {
								this.$u.toast('头像更换失败')
							}
						});
					}
				});
			},
			openPage(path, type) {
				if (!path) return
				let url = !!type ? path + '?majorType=' + type : path
				uni.navigateTo({
					url: url,
					fail: (err) => {
						this.$u.toast("暂无此页面")
					}
				})
			},
			personalPage(path) {
				if (!path) return
				uni.navigateTo({
					url: path + '?baseInfo=' + encodeURIComponent(JSON
						.stringify(this.baseInfo))
				})
			},
			isJSON(str) {
				try {
					var obj = JSON.parse(str);
					if (typeof obj == 'object' && obj) {
						return true;
					} else {
						return false;
					}
				} catch (e) {
					return false;
				}
			},
			scanCode() {
				uni.scanCode({
					success: res => {
						if (this.isJSON(res.result)) {
							const result = JSON.parse(res.result)
							if (result.t === 'ADP') {
								uni.navigateTo({
									url: '/pages/apply/dynamicModel/index?isPreview=1&id=' + result
										.id,
									fail: (err) => {
										this.$u.toast("暂无此页面")
									}
								})
							}
							if (result.t === 'DFD') {
								let config = JSON.stringify(result)
								uni.navigateTo({
									url: '/pages/apply/dynamicModel/scanForm?config=' + config,
									fail: (err) => {
										this.$u.toast("暂无此页面")
									}
								})
							}
							if (result.t === 'WFP') {
								uni.navigateTo({
									url: '/pages/workFlow/scanForm/index?id=' + result
										.id,
									fail: (err) => {
										this.$u.toast("暂无")
									}
								})
							}
							if (result.t === 'report') {
								let url =
									`${this.report}/preview.html?id=${result.id}&token=${this.token}&page=1&from=menu`
								uni.navigateTo({
									url: '/pages/apply/externalLink/index?url=' + encodeURIComponent(
										url),
									fail: (err) => {
										this.$u.toast("暂无")
									}
								})
							}
						} else {
							uni.navigateTo({
								url: '/pages/my/scanResult/index?result=' + res.result,
								fail: (err) => {
									this.$u.toast("暂无此页面")
								}
							})
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f0f2f6;
	}
.out{
	width: 100%;
	height: 80rpx;
	background-color: #fff;
	color: #FF3C3C ;
	line-height: 80rpx;
	text-align: center;
	margin-top: 20rpx;
	border-radius: 16rpx;
}
	.my-v {
		.user-box {
			background-color: #1776CE;
			color: #fff;
		}

		.f-right {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			text-align: center;
		}
	}
</style>
